﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using System.Web;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNManifestDataObjectParseWorker
	{
		
		public static void  parseDataObjectList(HttpContext context, Manifest manifest)
		{
			XmlElement root = manifest.Doc.DocumentElement;
			string xsfPrefix = manifest.XsfPrefix;
			
			ArrayList dataObjectList = new ArrayList();
			
			StringBuilder filterBuffer = new StringBuilder();
			filterBuffer.Append("//");
			filterBuffer.Append(NamespaceUtil.toFullName(xsfPrefix, "dataObjects"));
			filterBuffer.Append("/");
			filterBuffer.Append(NamespaceUtil.toFullName(xsfPrefix, "dataObject"));

			XmlNodeList dataObjectElementList = DOMUtil.selectNodes(root, filterBuffer.ToString(), 0, manifest.NsResolver);
			foreach (XmlElement dataObjectElement in dataObjectElementList)
			{
				DataObject dataObject = new DataObject();
				string name = dataObjectElement.GetAttribute( "name" );
				if( name == null || name.Length <= 0 )
				{
					throw new InfoJetException("No dataObject name", "XDOP01");
				}
				dataObject.Name = name;

				string schema = dataObjectElement.GetAttribute( "schema" );
				if( schema == null || schema.Length <= 0 )
				{
					throw new InfoJetException("No dataObject schema", "XDOP02");
				}
				dataObject.Schema = schema;

				string initOnLoad = dataObjectElement.GetAttribute( "initOnLoad" );
				if( initOnLoad != null && initOnLoad.Equals( "yes" ) )
				{
					dataObject.InitOnLoad = true;	
				}
				else
				{
					dataObject.InitOnLoad = false;
				}

				string queryFilter = NamespaceUtil.toFullName(xsfPrefix, "query");
				XmlElement queryElement = (XmlElement) DOMUtil.selectANode( dataObjectElement, queryFilter, manifest.NsResolver );
				if( queryElement != null ){
					Object dataAdapter = XSNManifestDataAdapterParseWorker.parseDataAdapterInContainer( context, manifest, queryElement );
					if( dataAdapter != null ){
						if( dataAdapter is DataADOAdapter ){
							dataObject.AdoAdapter = (DataADOAdapter)dataAdapter;
						}
						if( dataAdapter is DataFileAdapter ){
							dataObject.FileAdapter = (DataFileAdapter) dataAdapter;
						}
						if( dataAdapter is DataWSAdapter ){
							dataObject.WSAdapter = (DataWSAdapter) dataAdapter;
						}
						if( dataAdapter is DataSPListAdapter ){
							dataObject.SPListAdapter = (DataSPListAdapter) dataAdapter;
						}
						dataObjectList.Add( dataObject );
					}
				}

			}
			
			manifest.DataObjectList = dataObjectList;
			//CID 0230 //2009.02.24 //Begin
			int index = 0;
			foreach( DataObject dataObject in manifest.DataObjectList )
			{
				dataObject.Id = index.ToString();
				index ++;
			}
			//CID 0230 //2009.02.24 //End

			linkDataObjectCache( manifest );
		}

		//将FormConfigDataObjectCache和DataObject关联到一起。
		private static void linkDataObjectCache( Manifest manifest )
		{
			foreach( DataObject dataObject in manifest.DataObjectList )
			{
				if( dataObject.AdoAdapter != null )
				{
					manifest.FormConfiguration.LinkDataObjectCache( dataObject );
				}
			}
		}

	}
}